<template>
  <div class="tab">
    <el-card>
      <div>
        <el-tabs v-model="activeName">
          <el-tab-pane label="用户管理" name="first">
            <!-- 新增按钮 -->
            <div class="addDepartment">
              <el-button
                type="primary"
                size="mini"
                class="el-icon-plus"
                @click="addDepartment"
              >
                新增部门
              </el-button>
            </div>
            <el-table :data="employeeList" border style="width: 100%">
              <!-- 序号 -->
              <el-table-column type="index" label="序号" width="120" />

              <!-- 角色名 -->
              <el-table-column prop="name" label="角色名" width="280" />

              <!-- 描述 -->
              <el-table-column prop="description" label="描述" width="800" />

              <!-- 操作 -->
              <el-table-column label="操作" width="380">
                <template slot-scope="{ row }">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="emitDepartment(row)">
                    修改
                  </el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <Pagination
              :handle-size-change="handleSizeChange"
              :handle-current-change="handleCurrentChange"
              :total="total"
            />
          </el-tab-pane>

          <!-- 配置管理 -->

          <el-tab-pane label="配置管理" name="second" class="company">
            <el-form :model="companyInfo" label-width="80px">
              <!-- 企业名称 -->
              <el-form-item label="企业名称">
                <el-input v-model="companyInfo.name" disabled />
              </el-form-item>

              <!-- 公司地址 -->
              <el-form-item label="公司地址">
                <el-input v-model="companyInfo.companyAddress" disabled />
              </el-form-item>

              <!-- 公司电话 -->
              <el-form-item label="公司电话">
                <el-input v-model="companyInfo.companyPhone" disabled />
              </el-form-item>

              <!-- 公司邮箱 -->
              <el-form-item label="公司邮箱">
                <el-input v-model="companyInfo.mailbox" disabled /></el-input></el-form-item>

              <!-- 备注 -->
              <el-form-item label="备注">
                <el-input v-model="companyInfo.remarks" disabled />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </div>

      <!-- 新增弹窗 -->
      <el-dialog title="新增弹窗" :visible.sync="userFormDialog">
        <el-form :model="userForm">
          <!-- 角色名称 -->
          <el-form-item label="角色名称" label-width="100px">
            <el-input v-model="userForm.name" />
          </el-form-item>

          <!-- 角色描述 -->
          <el-form-item label="角色描述" label-width="100px">
            <el-input v-model="userForm.description" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="userFormDialog = false">取 消</el-button>
          <el-button @click="addSubmit"> 确 定 </el-button>
        </div>
      </el-dialog>

    </el-card>
  </div>
</template>

<script lang="js">
import {
  // 获取企业列表
  getEnterpriseListApi,
  //  添加角色
  getAddRoleApi,
  // 根据ID更新角色
  getEmitRoleApi,
  // 根据ID删除角色
  getDelRoleApi,
  // 查询全部企业列表
  getCompanyInfoApi
} from '@/api/approvalsApi.js'
export default {
  name: 'Approvals',
  data() {
    return {
      activeName: 'first',
      page: 1,
      pagesize: 10,
      employeeList: [],
      total: 1,
      userForm: {
        name: '',
        description: '',
        id: ''
      },
      userFormDialog: false,
      companyInfo: {
        // name: ''
      }
    }
  },
  mounted() {
    this.getEnterpriseList()
    this.getCompanyInfo()
  },
  methods: {
    // 根据ID删除角色
    del(row) {
      this.$confirm('确定删除吗？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'

      }).then(async() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        const res = await getDelRoleApi(row.id)
        this.getEnterpriseList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 查询全部企业列表
    async  getCompanyInfo() {
      const res = await getCompanyInfoApi()
      this.companyInfo = res.data[0]
      console.log(res)
    },
    // 修改面板
    async emitDepartment(row) {
      this.userFormDialog = true
      this.userForm = { ...row }
    },
    // 提交新增
    async addSubmit() {
      // 有id 就是修改
      if (this.userForm.id) {
        const res = await getEmitRoleApi(this.userForm)
        this.getEnterpriseList()
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.userFormDialog = false
        this.userForm = {
          name: '',
          description: ''
        }
      } else {
        const res = await getAddRoleApi(this.userForm)
        this.getEnterpriseList()
        this.userFormDialog = false
        this.userForm = {
          name: '',
          description: ''
        }
      }
    },

    // 获取企业列表
    async  getEnterpriseList() {
      const res = await getEnterpriseListApi({
        page: this.page,
        pagesize: this.pagesize
      })
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    // 打开新增面板
    addDepartment() {
      this.userFormDialog = true
    },
    // 分页容量
    handleSizeChange(size) {
      this.pagesize = size
      this.getEnterpriseList()
    },
    handleCurrentChange(page) {
      this.page = page
      this.getEnterpriseList()
    }
  }
}
</script>

<style lang="scss" scope>
.tab {
  margin: 40px 40px;
  .addDepartment {
    margin-bottom: 10px;
  }
}
.company {
  margin-left: 80px;
  width: 500px;
}
</style>
